package yk.com;

import java.util.ArrayList;

public class StudentManager {
    private static ArrayList<Student> students = new ArrayList<>();

    public static void addStudent(Student student) {
        students.add(student);
        System.out.println("学生信息添加成功！");
    }

    public static void removeStudent(String id) {
        boolean found = false;
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)) {
                students.remove(i);
                found = true;
                System.out.println("学生信息删除成功！");
                break;
            }
        }

        if (!found) {
            System.out.println("未找到该学号的学生！");
        }
    }

    public static void removeAllStudents() {
        if (students.isEmpty()) {
            System.out.println("当前系统中没有学生信息！");
            return;
        }

        students.clear();
        System.out.println("所有学生信息已删除！");
    }

    public static void modifyStudent(String oldId, Student newStudent) {
        boolean found = false;
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(oldId)) {
                found = true;
                students.set(i, newStudent);
                System.out.println("学生信息修改成功！");
                break;
            }
        }

        if (!found) {
            System.out.println("未找到该学号的学生！");
        }
    }

    public static void showAllStudents() {
        if (students.isEmpty()) {
            System.out.println("当前系统中没有学生信息！");
            return;
        }

        // 信息表格
        System.out.println("+----------+----------+------------+----------+");
        System.out.println("| 学号      | 姓名      | 年龄      | 居住地      |");
        System.out.println("+----------+----------+------------+----------+");

        // 循环遍历学生集合
        for (Student student : students) {
            System.out.printf("| %-8s | %-8s | %-7s岁 | %-8s |\n",
                    student.getId(),
                    student.getName(),
                    student.getAge(),
                    student.getAddress());
        }

        System.out.println("+----------+----------+------------+----------+");
    }

    public static void showStudentById(String id) {
        Student student = null;
        for (Student s : students) {
            if (s.getId().equals(id)) {
                student = s;
                break;
            }
        }

        if (student != null) {
            System.out.println("查询到的学生信息如下：");
            System.out.println("+----------+----------+------------+----------+");
            System.out.println("| 学号      | 姓名      | 年龄      | 居住地      |");
            System.out.println("+----------+----------+------------+----------+");
            System.out.printf("| %-8s | %-8s | %-7s岁 | %-8s |\n",
                    student.getId(),
                    student.getName(),
                    student.getAge(),
                    student.getAddress());
            System.out.println("+----------+----------+------------+----------+");
        } else {
            System.out.println("未找到该学号的学生！");
        }
    }

    public static boolean isStudentIdExists(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }
}
